﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.TipsCommands
{
    public class GestTipsSearchHandler : IRequestHandler<GestTipsSearchCommand, Result>
    {
        private readonly IBaseRepository<SleepInfor> sleepinforbaseRepository;
        private readonly IBaseRepository<Cottages> cottagebaseRepository;
        private readonly IBaseRepository<Fields> fieldsRepository;

        public GestTipsSearchHandler(IBaseRepository<SleepInfor> sleepinforbaseRepository, IBaseRepository<Fields> fieldsRepository, IBaseRepository<Cottages> cottagebaseRepository)
        {
            this.sleepinforbaseRepository = sleepinforbaseRepository;
            this.fieldsRepository = fieldsRepository;
            this.cottagebaseRepository = cottagebaseRepository;
        }
        /// <summary>
        /// 业务逻辑（分娩提示）
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(GestTipsSearchCommand request, CancellationToken cancellationToken)
        {
            var result=new Result() { Code=(int)CodeEnum.search};
            var list = (from a in sleepinforbaseRepository.GetAll()
                        join b in cottagebaseRepository.GetAll() on a.RecordcottageId equals b.CottagesId
                        join c in fieldsRepository.GetAll() on a.FieIdId equals c.FieIdId
                        where a.Gender == 2 && a.BreedStatuId==6
                        select new TipsDto()
                        {
                            Gender = a.Gender,
                            BreedStatuId = a.BreedStatuId,
                            EarNumber = a.EarNumber,
                            SleepEntryDate = a.SleepEntryDate,
                            CottageName = b.CottageName,
                            FieIdName = c.FieIdName,
                        }).ToList();
            result.Data = list;
            result.Count = list.Count;
            return result;
        }
    }
}
